package com.compass.doctor.ui.home.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.compass.doctor.R
import com.compass.doctor.databinding.LayoutArticleItemBinding
import com.compass.doctor.databinding.LayoutSpecialColumnArticleItemBinding
import com.compass.framework.adapter.BaseBindViewHolder
import com.compass.framework.adapter.BaseMultiItemAdapter
import com.compass.framework.ext.saveAs
import com.compass.framework.glide.setUrl
import com.compass.framework.model.ArticleOrVideo
import com.compass.framework.utils.DateUtils
import com.compass.framework.utils.ViewUtils
import com.compass.framework.utils.dpToPx
import com.compass.framework.utils.getDrawableResource


/**
 * Description： 文章和视频adapter
 * Date:2023/12/24 13:56
 */

class ArticleAndVideoAdapter constructor(private var itemType: Int) :
    BaseMultiItemAdapter<ArticleOrVideo>() {

        var articleOrVideo=1;//1是文章 2是视频

    //1、知识中心  2.专栏列表的
    override fun getDefItemViewType(position: Int): Int {
        return itemType
    }


    fun setArticleOrVideoType(articleOrVideo: Int) {
        this.articleOrVideo = articleOrVideo
    }

    override fun onBindDefViewHolder(
        holder: BaseBindViewHolder<ViewBinding>,
        item: ArticleOrVideo?,
        position: Int
    ) {
        if (item == null) return
        //1、知识中心  2.专栏列表的
        when (holder.itemViewType) {
            1 -> {
                val binding = holder.binding.saveAs<LayoutArticleItemBinding>()
                binding.apply {
                    if (position == getData().size - 1) lineBottom.visibility =
                        View.GONE else lineBottom.visibility = View.VISIBLE
                    tvTitle.text = item.title
                    tvReadNum.text = "浏览量 ${item.readNum}"
                    tvLike.text = "点赞 ${item.likeNum}"
                    tvTime.text = DateUtils.getTimeDescriptionByDate(item.createTime)
                    ivCoverImg.setUrl(item.coverImg)
                    ViewUtils.setClipViewCornerRadius(ivCoverImg, dpToPx(10))
                    imgVideo.visibility= if (articleOrVideo==1) View.GONE else View.VISIBLE
                    if (item.tag==1) {
                        tvTag.text="健康科普"
                        tvTag.background= getDrawableResource(R.drawable.bg_special_tag_one)
                    }else{
                        tvTag.text="诊疗案例"
                        tvTag.background= getDrawableResource(R.drawable.bg_special_tag_two)
                    }
                }

            }

            else -> {
                val binding = holder.binding.saveAs<LayoutSpecialColumnArticleItemBinding>()
                binding.apply {
                    tvTitle.text = item.title
                    tvReadNum.text = "浏览量 ${item.readNum}"
                    tvLike.text = "点赞 ${item.likeNum}"
                    tvTime.text = DateUtils.getTimeDescriptionByDate(item.createTime)
                    ivCoverImg.setUrl(item.coverImg)
                    ViewUtils.setClipViewCornerRadius(ivCoverImg, dpToPx(10))
                    if (item.tag==1) {
                        tvTag.text="健康科普"
                        tvTag.background= getDrawableResource(R.drawable.bg_special_tag_one)
                    }else{
                        tvTag.text="诊疗案例"
                        tvTag.background= getDrawableResource(R.drawable.bg_special_tag_two)
                    }

                    imgVideo.visibility= if (articleOrVideo==1) View.GONE else View.VISIBLE
                    tvTag.visibility= if (articleOrVideo==1) View.VISIBLE else View.GONE
                }
            }
        }
    }


    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ViewBinding {
        return when (viewType) {
            1 -> {
                LayoutArticleItemBinding.inflate(layoutInflater, parent, false)
            }

            else -> {
                LayoutSpecialColumnArticleItemBinding.inflate(layoutInflater, parent, false)
            }
        }

    }
}